System.import('components/foobar').then(function(foobar) {
// access to the module you loaded.
// e.g. access to a component's ViewModel
// foobar.ViewModel
});
Please notice that when dynamically importing modules in a stache file, the scope inside <can-import> is a Promise, so you have to wait until it is resolved.
Use the {{#if isResolved}} helper for that.
The recommended way to progressively load modules with StealJS is to use the @loader module provided by steal.
For more information see http://stealjs.com/docs/@loader.html
working with modules
With the new CanJS binding syntax introduced in 2.3 you can use can-import to work with modules like in ES6 or CJS.
For example we have a ES6 module:
mymodule.js
let test = "default export value";
export function doMath(number1, number2) {
return parseInt(number1, 10) + parseInt(number2, 10);
};
export default test;
One way is to import the module within the reference scope:
app.js
As you may notice, you have to define a can.Map with predefined attributes. The best way to do that is to use the define plugin.
To load a function from a module you have to use the @ operator. For more information see the documentation
can-tag
can-tag allows for injecting a component, using the imported promise as the
injected component's view model.
The example below shows a loading graphic until the cart component has been loaded.
Once the cart promise is resolved, <shopping-cart></shopping-cart> is injected
into the page.
Use
A template might depend on component or helper modules.
<can-import>allows you to specify these dependencies.Example:
Currently this only works with can.autorender or the system plugin.
Progressive Loading
A template may load or conditionally load a module after the initial page load.
<can-import>allows progressive loading by using an end tag.The first example below shows a component being loaded ad hoc. The second illustrates conditionally loading modules based on some application state.
Example:
static & dynamic import
static
These are imports that are direct dependencies of a template.
Example:
which is equivalent to a ES6 import like:
dynamic
These are conditional imports, things you only want to import in certain situations. Like described before in section "Progressive Loading".
which is equivalent to a stealJS import like:
Please notice that when dynamically importing modules in a stache file, the scope inside <can-import> is a Promise, so you have to wait until it is resolved. Use the
{{#if isResolved}}helper for that.The recommended way to progressively load modules with StealJS is to use the
@loadermodule provided by steal. For more information see http://stealjs.com/docs/@loader.htmlworking with modules
With the new CanJS binding syntax introduced in 2.3 you can use
can-importto work with modules like in ES6 or CJS. For example we have a ES6 module: mymodule.jsOne way is to import the module within the reference scope: app.js
The other way is to import the module into the template's data. app.js
As you may notice, you have to define a
can.Mapwith predefined attributes. The best way to do that is to use the define plugin. To load a function from a module you have to use the@operator. For more information see the documentationcan-tag
can-tagallows for injecting a component, using the imported promise as the injected component's view model.The example below shows a loading graphic until the cart component has been loaded. Once the cart promise is resolved,
<shopping-cart></shopping-cart>is injected into the page.Loading Indicator Component
Main Template